from django.db import models
from django.utils.text import slugify

class Category(models.Model):
    name = models.CharField(max_length=100, verbose_name="分类名称")
    slug = models.SlugField(max_length=191, unique=True, verbose_name="分类别名")
    description = models.TextField(blank=True, verbose_name="分类描述")
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, related_name='children', verbose_name="父分类")
    order = models.IntegerField(default=0, verbose_name="排序")
    
    class Meta:
        verbose_name = "分类"
        verbose_name_plural = "分类"
        ordering = ['order']
    
    def __str__(self):
        return self.name
    
    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.name)
        super().save(*args, **kwargs)

class Solution(models.Model):
    title = models.CharField(max_length=191, verbose_name="解决方案标题")
    slug = models.SlugField(max_length=191, unique=True, verbose_name="解决方案别名")
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="solutions", verbose_name="所属分类")
    short_description = models.TextField(verbose_name="简短描述")
    content = models.TextField(verbose_name="详细内容")
    image = models.ImageField(upload_to='solutions/', blank=True, null=True, verbose_name="封面图片")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    is_featured = models.BooleanField(default=False, verbose_name="是否精选")
    order = models.IntegerField(default=0, verbose_name="排序")
    
    class Meta:
        verbose_name = "解决方案"
        verbose_name_plural = "解决方案"
        ordering = ['order']
    
    def __str__(self):
        return self.title
    
    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
        super().save(*args, **kwargs)

class Insight(models.Model):
    title = models.CharField(max_length=191, verbose_name="洞察标题")
    slug = models.SlugField(max_length=191, unique=True, verbose_name="洞察别名")
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="insights", verbose_name="所属分类")
    short_description = models.TextField(verbose_name="简短描述")
    content = models.TextField(verbose_name="详细内容")
    image = models.ImageField(upload_to='insights/', blank=True, null=True, verbose_name="封面图片")
    publication_date = models.DateField(verbose_name="发布日期")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    is_featured = models.BooleanField(default=False, verbose_name="是否精选")
    
    class Meta:
        verbose_name = "行业洞察"
        verbose_name_plural = "行业洞察"
        ordering = ['-publication_date']
    
    def __str__(self):
        return self.title
    
    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
        super().save(*args, **kwargs)

class Event(models.Model):
    title = models.CharField(max_length=191, verbose_name="活动标题")
    slug = models.SlugField(max_length=191, unique=True, verbose_name="活动别名")
    short_description = models.TextField(verbose_name="简短描述")
    content = models.TextField(verbose_name="详细内容")
    image = models.ImageField(upload_to='events/', blank=True, null=True, verbose_name="封面图片")
    event_date = models.DateTimeField(verbose_name="活动日期")
    location = models.CharField(max_length=191, verbose_name="活动地点")
    is_online = models.BooleanField(default=False, verbose_name="是否线上活动")
    registration_link = models.URLField(blank=True, null=True, verbose_name="报名链接")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    is_featured = models.BooleanField(default=False, verbose_name="是否精选")
    
    class Meta:
        verbose_name = "活动"
        verbose_name_plural = "活动"
        ordering = ['-event_date']
    
    def __str__(self):
        return self.title
    
    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.title)
        super().save(*args, **kwargs)

class Contact(models.Model):
    name = models.CharField(max_length=100, verbose_name="姓名")
    email = models.EmailField(verbose_name="电子邮箱")
    phone = models.CharField(max_length=20, verbose_name="电话")
    company = models.CharField(max_length=100, verbose_name="公司")
    position = models.CharField(max_length=100, verbose_name="职位")
    message = models.TextField(verbose_name="留言内容")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    
    class Meta:
        verbose_name = "联系表单"
        verbose_name_plural = "联系表单"
        ordering = ['-created_at']
    
    def __str__(self):
        return f"{self.name} - {self.company}"

class Banner(models.Model):
    title = models.CharField(max_length=191, verbose_name="标题")
    subtitle = models.CharField(max_length=191, blank=True, null=True, verbose_name="副标题")
    image = models.ImageField(upload_to='banners/', verbose_name="横幅图片")
    link = models.URLField(blank=True, null=True, verbose_name="链接")
    button_text = models.CharField(max_length=50, blank=True, null=True, verbose_name="按钮文本")
    order = models.IntegerField(default=0, verbose_name="排序")
    is_active = models.BooleanField(default=True, verbose_name="是否激活")
    
    class Meta:
        verbose_name = "横幅图片"
        verbose_name_plural = "横幅图片"
        ordering = ['order']
    
    def __str__(self):
        return self.title

class Office(models.Model):
    name = models.CharField(max_length=100, verbose_name="办公室名称")
    address = models.TextField(verbose_name="地址")
    phone = models.CharField(max_length=20, verbose_name="电话")
    email = models.EmailField(verbose_name="电子邮箱")
    is_headquarters = models.BooleanField(default=False, verbose_name="是否总部")
    
    class Meta:
        verbose_name = "办公地点"
        verbose_name_plural = "办公地点"
    
    def __str__(self):
        return self.name
